<?php

if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );

/**
 * OPE library class
 *
 * OPE related functions.
 */
class Deploy_lib {
	/**
	 * Constructor
	 *
	 * Load all account model.
	 */
	public function __construct() {
		$this->CI = &get_instance ();
		$this->CI->load->model ( 'deploy/account_model' );
/*
		$this->CI->load->model ( 'deploy/appinfo_model' );
		$this->CI->load->model ( 'deploy/chcode_params_model' );
		$this->CI->load->model ( 'deploy/app_encrypt_model' );
*/
	}
	
	// Generate token regarding remote
	// ip and uid
	private function _generate_token($remote_ip, $uid) {
		$pid = function_exists ( "posix_getpid" ) ? posix_getpid () : get_current_user ();
		$token = hash_hmac ( 'sha256', '', implode ( "\t", array (
				time (),
				rand (),
				$pid,
				$remote_ip,
				$uid 
		) ) );
		return $token;
	}
	
	/**
	 * get account according uid
	 *
	 * @param
	 *        	integer	uid
	 * @return array account information
	 */
	public function get_account_by_id($uid) {
		if (empty ( $uid ))
			return array ();
		
		$account = $this->CI->account_model->get_user_by_uid ( $uid );
		if (! empty ( $account ) || $account ['status'] == 1 && $account ['expire'] > time ())
			return $account;
		
		return array ();
	}
	
	/**
	 * Get account's information
	 * regarding user name and password
	 *
	 * @param
	 *        	string	user name
	 * @param
	 *        	string	password
	 * @return array account information
	 */
	public function get_account($uname, $pwd) {
		if (empty ( $uanme ) && empty ( $pwd ))
			return array ();
		
		$user = $this->CI->account_model->get_user_by_name ( $uname );
		if (empty ( $user ))
			return array ();
		
		if (strcmp ( $user->pwd, sha1 ( $pwd . $user->salt ) ) == 0)
			return $user;
		
		return array ();
	}
	
	/**
	 * get chcode params template
	 * 
	 * @param unknown $chcodes        	
	 * @return multitype:unknown
	 *         |unknown
	 */
	public function get_params($chcodes) {
		$result = array ();
		$chcode_params = $this->CI->chcode_params_model->get_params_by_chcodes ( $chcodes );
		if (is_array ( $chcodes )) {
			foreach ( $chcode_params as $row ) {
				$result [$row ['chcode']] = $row ['params'];
				$result [$row ['chcode'] . '_icon'] = $row ['icon_flag'];
				$result [$row ['chcode'] . '_group'] = $row ['group_id'];
			}
			return $result;
		}
		return $chcode_params;
	}
	
	/**
	 * get appid chcode params
	 * 
	 * @param unknown $appid        	
	 * @param unknown $chcodes        	
	 * @return multitype:unknown
	 *         |unknown
	 */
	public function get_app_params($appid, $chcodes) {
		$result = array ();
		$chcode_params = $this->CI->app_chcode_params_model->get_params_by_chcodes ( $appid, $chcodes );
		if (is_array ( $chcodes )) {
			foreach ( $chcode_params as $row ) {
				$result [$row ['chcode']] = $row ['params'];
			}
			return $result;
		}
		return $chcode_params;
	}
	
/**
         * get appid chcode encrypt flag
         * 
         * @param unknown $appid                
         * @param unknown $chcodes              
         * @return multitype:unknown
         *         |unknown
         */
        public function get_app_encrypt($appid, $chcode) {
                $result = array ();
                $chcode_param = $this->CI->app_encrypt_model->get_encrypt_extra_chcode_by_appid($appid);
		foreach($chcode_param as $k => $v)
		$extra_chcodes = $v;
		$extra_chcode = explode(',',$extra_chcodes);
                if (is_array ( $extra_chcode )) {
                        foreach ( $extra_chcode as $k => $v ) {
					if(trim($v)==$chcode){
						return 0;
					}else{
						continue;
					}
			}
			return 1;
		}
	}	
	/**
	 * get chcode group names
	 * 
	 * @param array $chcode_groups        	
	 * @return array $return
	 */
	public function get_chcode_group_names($chcode_groups) {
		$result = array ();
		foreach ( $chcode_groups as $group ) {
			$result [$group ['group_id']] = $group ['name'];
		}
		return $result;
	}
	
	/**
	 * get chcode list
	 * 
	 * @param unknown $chcodes        	
	 * @return multitype:unknown
	 */
	public function get_chcode_list($chcodes) {
		$result = array ();
		foreach ( $chcodes as $chcode ) {
			if (isset ( $chcode ['chcode'] )) {
				$result [] = $chcode ['chcode'];
			}
		}
		return $result;
	}
}
